﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Linq;
using System.Windows.Data;
using CloudBackupLib;

namespace CloudBackup
{
    // Class VirtualMachineProvider
    //
    internal class VirtualMachineProvider
    {
        private static ObservableCollection<VirtualMachine> _CachedLocal;
        private static ObservableCollection<VirtualMachine> _CachedRemote;

        private static object _Lock = new object();

        public static ICollection<VirtualMachine> GetLocalVirtualMachines(bool useCached)
        {
            lock(_Lock)
            {
                if(_CachedLocal == null)
                {
                    _CachedLocal = new ObservableCollection<VirtualMachine>(VirtualMachine.GetLocalVirtualMachines());
                }
                else if(useCached == false)
                {
                    _CachedLocal.Clear();
                    VirtualMachine.GetLocalVirtualMachines().ForEach(vm => _CachedLocal.Add(vm));                    
                }
            }

            return _CachedLocal;
        }

        public static ICollection<VirtualMachine> GetRemoteVirtualMachines(bool useCached)
        {
            lock(_Lock)
            {
                if(_CachedRemote == null)
                {
                    _CachedRemote = new ObservableCollection<VirtualMachine>(VirtualMachine.GetRemoteVirtualMachines());
                }
                else if(useCached == false)
                {
                    _CachedRemote.Clear();
                    VirtualMachine.GetRemoteVirtualMachines().ForEach(vm => _CachedRemote.Add(vm));
                }
            }

            return _CachedRemote;
        }        
    }

    // Class VirtualMachineImageConverter
    //
    [ValueConversion(typeof(VirtualMachine.State), typeof(string))]
    internal class VirtualMachineImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            VirtualMachine virtualMachine = (VirtualMachine)value;

            // Figure out if the local VM has a backup
            //
            bool hasBackup = false;
            if(virtualMachine.IsLocal)
            {
                hasBackup = VirtualMachineProvider.GetRemoteVirtualMachines(true).Where(vm => vm.Name == virtualMachine.Name).FirstOrDefault() != null;
            }

            switch(virtualMachine.CurrentState)
            {
                case VirtualMachine.State.Running:
                {
                    if(hasBackup)
                    {
                        return "images/server_running1.png";
                    }
                    else
                    {
                        return "images/nb_server_running1.png";
                    }
                }
                case VirtualMachine.State.Stopped:
                case VirtualMachine.State.Saved:
                {
                    if(hasBackup)
                    {
                        return "images/server_stopped1.png";
                    }
                    else
                    {
                        return "images/nb_server_stopped1.png";
                    }
                }
                default:
                {
                    return "images/server1.png";
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            VirtualMachine.State state = VirtualMachine.State.Invalid;
            return state;
        }
    }


}

